package com.mew.chess.core.game.shbz.game.task;

import com.mew.chess.core.common.handler.ChessPushMessageUtil;
import com.mew.chess.core.common.handler.GameCommonService;
import com.mew.chess.core.game.shbz.game.ShbzGameManager;
import com.mew.chess.core.game.shbz.game.ShbzTableManager;
import com.mew.chess.core.game.shbz.game.common.FishBO;
import com.mew.chess.core.game.shbz.game.common.FishConstantValue;
import com.mew.chess.core.game.shbz.game.common.FishUserBO;
import com.mew.chess.core.game.shbz.game.message.FishCMDMapType;
import com.mew.chess.core.game.shbz.game.message.TidePushMessage;
import com.mew.common.rest.bo.GameSession;
import com.mew.common.util.TxtUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.stream.Collectors;

@Slf4j
@Component
public class TideScheduleTask {

    @Autowired
    private GameCommonService gameCommonService;

    public boolean fishCycle(int roomId, int table,int type){

        ShbzTableManager tableManager = ShbzGameManager.getInstance().getController(roomId).getTable(table);

        tableManager.getOnlineUserMap().forEach((k, v) -> {
            GameSession session = v.getGameSession();
            if (null == session || session.isWeakClose()){
                tableManager.clearOnlineUser(k);
            }
        });

        gameCommonService.checkTMSOnline(tableManager.getOnlineUserMap().values().stream().map(FishUserBO::getGameSession).collect(Collectors.toList()), ShbzGameManager.getInstance().getGameId(), tableManager.getRoomId());

        if (tableManager.getOnlineUserMap().isEmpty()){
            log.info("Fish roomId {} not real user is stop ", roomId);
            tableManager.stopGame();
            return false;
        }

        //清理数据
        tableManager.allFishClear();

        //顺序鱼潮
//        int oldTide = tableManager.getOldTide();
//        oldTide = oldTide>=5?0:oldTide+1;
//        tableManager.setOldTide(oldTide);

        startGameLinkType(tableManager,type);

        tideType(tableManager,type);

        sendStartMsg(tableManager,type);

        return true;
    }


    public void startGameLinkType(ShbzTableManager tableManager,int type){
        tableManager.setRuleTime(FishConstantValue.FISH_TIDE_CYCLE[type-101]);
        tableManager.setLinkType(type);
        tableManager.setStartTime(System.currentTimeMillis());
    }

    private void sendStartMsg(ShbzTableManager tableManager,int type){
        TidePushMessage message = tableManager.getTidePushMessage();
        tableManager.getOnlineUserMap().forEach((k, v) -> {
            GameSession session = v.getGameSession();
            ChessPushMessageUtil.pushMessage(session, FishCMDMapType.TideStartMessage.code(), message);
            log.info("shbz start tide push uid{} type{}",session.getUserId(),type);
        });
    }

    public void tideType(ShbzTableManager tableManager,int type){

        TidePushMessage tidePushMessage = tableManager.getTidePushMessage();
        tidePushMessage.setType(type);
        tidePushMessage.setStartTime(tableManager.getStartTime());
        tidePushMessage.setScenarioNo(tableManager.getScenarioNo());
        tidePushMessage.setStartTime(new Date().getTime());

        if (type==101){
            type101(tableManager);
        }else if (type==102){
            type102(tableManager);
        }else if (type==103){
            type103(tableManager);
        }else if (type==104){
            type104(tableManager);
        }else if (type==105){
            type105(tableManager);
        }else if (type==106){
            type106(tableManager);
        }

        tidePushMessage.setFishBOs(new ArrayList<>(tableManager.getFishMap().values()));
        System.out.println("----------------");
        System.out.println("----------------");
        System.out.println(tidePushMessage.getFishBOs().size());
    }

    public void creatTide(ShbzTableManager tableManager,int routeId,int fishType,int fishNum){
        for (int i = 0; i < fishNum; i++) {
            FishBO bo = FishBO.creat(tableManager.getFishKeyId().getAndIncrement(), fishType, routeId,i);
            tableManager.getFishMap().put(bo.getFishId(),bo);
        }
    }



    public void type101(ShbzTableManager tableManager){
        creatTide(tableManager,1,2,4);
        creatTide(tableManager,0,2,4);
        creatTide(tableManager,1,2,4);
        creatTide(tableManager,2,2,4);
        creatTide(tableManager,3,2,4);
        creatTide(tableManager,4,2,4);
        creatTide(tableManager,5,2,4);
        creatTide(tableManager,6,2,4);
        creatTide(tableManager,7,2,4);
        creatTide(tableManager,8,2,4);
        creatTide(tableManager,9,2,4);
        creatTide(tableManager,10,2,4);
        creatTide(tableManager,11,2,4);
        creatTide(tableManager,12,2,4);
        creatTide(tableManager,13,2,4);
        creatTide(tableManager,14,2,4);
        creatTide(tableManager,2,7,4);
        creatTide(tableManager,15,7,4);
        creatTide(tableManager,16,7,4);
        creatTide(tableManager,17,7,4);
        creatTide(tableManager,18,7,4);
        creatTide(tableManager,19,7,4);
        creatTide(tableManager,20,7,4);
    }

    public void type102(ShbzTableManager tableManager){
        creatTide(tableManager,1,8,4);
        creatTide(tableManager,0,8,4);
        creatTide(tableManager,1,8,4);
        creatTide(tableManager,2,8,4);
        creatTide(tableManager,3,8,4);
        creatTide(tableManager,4,8,4);
        creatTide(tableManager,5,8,4);
        creatTide(tableManager,6,8,4);
        creatTide(tableManager,7,8,4);
        creatTide(tableManager,8,8,4);
        creatTide(tableManager,9,8,4);
        creatTide(tableManager,10,8,4);
        creatTide(tableManager,11,8,4);
        creatTide(tableManager,12,8,4);
        creatTide(tableManager,13,8,4);
        creatTide(tableManager,2,12,4);
        creatTide(tableManager,14,12,4);
        creatTide(tableManager,15,12,4);
        creatTide(tableManager,16,12,4);
        creatTide(tableManager,17,12,4);
        creatTide(tableManager,18,12,4);
        creatTide(tableManager,19,12,4);
        creatTide(tableManager,20,12,4);
        creatTide(tableManager,21,12,4);
    }

    public void type103(ShbzTableManager tableManager){
        creatTide(tableManager,0,14,1);
        creatTide(tableManager,1,14,1);
        creatTide(tableManager,2,14,1);
        creatTide(tableManager,3,14,1);
        creatTide(tableManager,1,5,1);
        creatTide(tableManager,4,5,1);
        creatTide(tableManager,5,5,1);
        creatTide(tableManager,6,5,1);
        creatTide(tableManager,7,5,1);
        creatTide(tableManager,8,5,1);
        creatTide(tableManager,9,5,1);
        creatTide(tableManager,10,5,1);
        creatTide(tableManager,11,5,1);
        creatTide(tableManager,12,5,1);
        creatTide(tableManager,13,5,1);
        creatTide(tableManager,14,5,1);
        creatTide(tableManager,15,5,1);
        creatTide(tableManager,16,5,1);
        creatTide(tableManager,17,5,1);
        creatTide(tableManager,2,5,1);
        creatTide(tableManager,18,5,1);
        creatTide(tableManager,19,5,1);
        creatTide(tableManager,20,5,1);
        creatTide(tableManager,21,5,1);
        creatTide(tableManager,22,5,1);
        creatTide(tableManager,23,5,1);
        creatTide(tableManager,24,5,1);
        creatTide(tableManager,25,5,1);
        creatTide(tableManager,26,5,1);
        creatTide(tableManager,27,5,1);
        creatTide(tableManager,28,5,1);
        creatTide(tableManager,29,5,1);
        creatTide(tableManager,30,5,1);
        creatTide(tableManager,31,5,1);
        creatTide(tableManager,3,5,1);
        creatTide(tableManager,32,5,1);
        creatTide(tableManager,33,5,1);
        creatTide(tableManager,34,5,1);
        creatTide(tableManager,35,5,1);
        creatTide(tableManager,36,5,1);
        creatTide(tableManager,37,5,1);
        creatTide(tableManager,38,5,1);
        creatTide(tableManager,39,5,1);
        creatTide(tableManager,40,5,1);
        creatTide(tableManager,41,5,1);
        creatTide(tableManager,42,5,1);
        creatTide(tableManager,43,5,1);
        creatTide(tableManager,44,5,1);
        creatTide(tableManager,45,5,1);
        creatTide(tableManager,4,5,1);
        creatTide(tableManager,46,5,1);
        creatTide(tableManager,47,5,1);
        creatTide(tableManager,48,5,1);
        creatTide(tableManager,49,5,1);
        creatTide(tableManager,50,5,1);
        creatTide(tableManager,51,5,1);
        creatTide(tableManager,52,5,1);
        creatTide(tableManager,53,5,1);
        creatTide(tableManager,54,5,1);
        creatTide(tableManager,55,5,1);
        creatTide(tableManager,56,5,1);
        creatTide(tableManager,57,5,1);
        creatTide(tableManager,58,5,1);
        creatTide(tableManager,59,5,1);
    }

    public void type104(ShbzTableManager tableManager){
        creatTide(tableManager,0,7,22);
        creatTide(tableManager,1,7,22);
        creatTide(tableManager,2,8,22);
        creatTide(tableManager,3,8,22);
    }

    public void type105(ShbzTableManager tableManager){
        creatTide(tableManager,0,12,30);
        creatTide(tableManager,1,12,30);
        creatTide(tableManager,2,15,6);
        creatTide(tableManager,3,15,6);
    }

    public void type106(ShbzTableManager tableManager){
        creatTide(tableManager,0,8,19);
        creatTide(tableManager,1,7,22);
        creatTide(tableManager,2,3,32);
        creatTide(tableManager,3,8,19);
        creatTide(tableManager,4,7,22);
        creatTide(tableManager,5,3,32);
        creatTide(tableManager,6,21,3);
    }


    @Test
    public void test1() {
        String txt = TxtUtils.byFile("external/roller/fish/tide.ini");
        txt = txt.replace("\n","")
                .replace("\r","")
                .replace("{","")
                .replace("}","")
                .replace("[","")
                .replace("]","")
                .replace("\"","")
                .replace(" ","")
        ;

        int m = 1;

        int idx = 0;
        int id = 0;
        int id2 = 0;
        int count = 0;
        int type = 0;

        int[] itc = new int[3];
        ArrayList<String[]> list = new ArrayList<>();
        do {
            String s = "creatTide(tableManager,";
            String e = ");";
            list.clear();

            id = txt.indexOf("id:");
            id2 = txt.indexOf("id:",id+1);
            count = txt.indexOf("fishingCount:");
            type = txt.indexOf("type:");

            if (id<0||count<0||type<0){
                break;
            }
            if (id2>0&&(id2<count||id2<type)){
                System.out.println("}\n\npublic void type10"+(m++)+"(ShbzTableManager tableManager){");
                idx = txt.length();
                txt = txt.substring(id2);
                continue;
            }


            list.add(new String[]{id+"","id"});
            list.add(new String[]{count+"","fishingCount"});
            list.add(new String[]{type+"","type"});

            Collections.sort(list,(a,b)->{
                return Integer.parseInt(a[0])-Integer.parseInt(b[0]);
            });

            for (String[] strings : list) {

                if ("id".equals(strings[1])){
                    itc[0] = Integer.parseInt(strings[0]);
                }else if ("type".equals(strings[1])){
                    itc[1] = Integer.parseInt(strings[0]);
                }else if ("fishingCount".equals(strings[1])){
                    itc[2] = Integer.parseInt(strings[0]);
                }


            }

            s+= txt.substring(itc[0]+3,txt.indexOf(",",itc[0]))+","+
                    txt.substring(itc[1]+5,txt.indexOf(",",itc[1]))+","+
                    txt.substring(itc[2]+13,txt.indexOf(",",itc[2]))+e;

            System.out.println("    "+s);

            idx = txt.length();
            txt = txt.substring(Integer.parseInt(list.get(2)[0])+1);

        }while (idx!=txt.length());
        System.out.println("}");

    }


}
